﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace kiosk
{
    public partial class BackGroundImages : Form
    {
        public BackGroundImages()
        {
            InitializeComponent();
        }

        
        


       







        private void BackGroundImages_Load(object sender, EventArgs e)
        {

            // Loading Images 
            // the Images names from the xml file
            XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath,GeneralOpt.AppSettingsFileName));
            if (docu.Root != null)
            { 
                XElement element = docu.Root.Element("BgImages");
               if (element != null)
                {
                   
                   XElement mainElement = element.Element("MainWindow");
                   XElement buttWin = element.Element("ButtonWindow");
                   XElement pdfWin = element.Element("PdfWindow");

                   if (mainElement != null)
                   { 
                       // Load the Images cand change the picture Box
                       this.mainBGImageBox.LoadAsync(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName, mainElement.Value));

                       

                   
                   }
                   if (buttWin != null)
                   {


                       this.ButtonBGImageBox.LoadAsync(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName, buttWin.Value));

                   }
                   if (pdfWin != null)
                   {

                       this.PdfBgImageBox.LoadAsync(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName, pdfWin.Value));

                   
                   
                   }
                   




                
                }
            
            
            
            }






        }

        private void mainBGLoadBtn_Click(object sender, EventArgs e)
        {



            this.openFileDlg.Filter = "Image Files(*.BMP;*.JPG;*.JPEG)|*.BMP;*.JPG;*.JPEG";
            if (this.openFileDlg.ShowDialog() == DialogResult.OK)
            {
                 
                String filepath = this.openFileDlg.FileName;
                // update the xml file 
                  if(!Directory.Exists(Path.Combine(Application.StartupPath,GeneralOpt.BackGroundImageFolder)))
                  {
                      Directory.CreateDirectory(Path.Combine(Application.StartupPath,GeneralOpt.BackGroundImageFolder));
                  
                  }

                  if (!Directory.Exists(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName)))
                  {
                      Directory.CreateDirectory(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName));

                  }
                
                  File.Copy(filepath,Path.Combine(Application.StartupPath,GeneralOpt.BackGroundImageFolder,this.openFileDlg.SafeFileName),true);

                  Image image = Bitmap.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder,this.openFileDlg.SafeFileName));

                  // Now resized to the thumbillwindow

                  MessageBox.Show("Please wait this will take somw time !!!");
                  Image resized = GeneralOpt.ImageResize(image,GeneralOpt.ThumBillWidth,GeneralOpt.ThumbillHeight);
                  resized.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName,this.openFileDlg.SafeFileName));
                  this.mainBGImageBox.Image = resized; 
                // update the xmlfile
                //
                  

                  XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath,GeneralOpt.AppSettingsFileName));
                  if (! docu.Root.IsEmpty)
                  {
                      XElement bgGround = docu.Root.Element("BgImages");
                      if (bgGround == null)
                      {
                          docu.Root.Add(new XElement("BgImages", new XElement("MainWindow", this.openFileDlg.SafeFileName)));

                      }
                      else
                      {
                          XElement main = bgGround.Element("MainWindow");
                          if (main == null)
                          {
                              bgGround.Add(new XElement("MainWindow", this.openFileDlg.SafeFileName));

                          }
                          else
                          {
                              main.SetValue(this.openFileDlg.SafeFileName);

                          }


                      }


                  }
                  else
                  {
                      
                  }
                  docu.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));


                  GeneralOpt.pictIDChange = 1;
                
                } // ok pressed





        }

        private void buttonWBgBtn_Click(object sender, EventArgs e)
        {

            //txt files (*.txt)|*.txt|All files (*.*)|*.*" 


            this.openFileDlg.Filter = "Image Files(*.BMP;*.JPG;*.JPEG)|*.BMP;*.JPG;*.JPEG";
            if (this.openFileDlg.ShowDialog() == DialogResult.OK)
            {

                String filepath = this.openFileDlg.FileName;
                // update the xml file 
                if (!Directory.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder)))
                {
                    Directory.CreateDirectory(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder));

                }

                if (!Directory.Exists(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName)))
                {
                    Directory.CreateDirectory(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName));

                }
                if (!File.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, this.openFileDlg.SafeFileName)))
                       File.Copy(filepath, Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, this.openFileDlg.SafeFileName), true);

                Image image = Bitmap.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, this.openFileDlg.SafeFileName));

                // Now resized to the thumbillwindow

                MessageBox.Show("Please wait this will take some time !!!");
                Image resized = GeneralOpt.ImageResize(image, GeneralOpt.ThumBillWidth, GeneralOpt.ThumbillHeight);
                resized.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName, this.openFileDlg.SafeFileName));
                this.ButtonBGImageBox.Image = resized;
                // update the xmlfile
                //
                image.Dispose();

                XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
                if (!docu.Root.IsEmpty)
                {
                    XElement bgGround = docu.Root.Element("BgImages");
                    if (bgGround == null)
                    {
                        docu.Root.Add(new XElement("BgImages", new XElement("ButtonWindow", this.openFileDlg.SafeFileName)));

                    }
                    else
                    {
                        XElement main = bgGround.Element("ButtonWindow");
                        if (main == null)
                        {
                            bgGround.Add(new XElement("ButtonWindow", this.openFileDlg.SafeFileName));

                        }
                        else
                        {
                            main.SetValue(this.openFileDlg.SafeFileName);

                        }


                    }


                }
                else
                {
                   
                }
                docu.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));



                GeneralOpt.pictIDChange = 2;
            } // ok pressed







        }

        private void pdfBgLoadBtn_Click(object sender, EventArgs e)
        {


            this.openFileDlg.Filter = "Image Files(*.BMP;*.JPG;*.JPEG)|*.BMP;*.JPG;*.JPEG";
            if (this.openFileDlg.ShowDialog() == DialogResult.OK)
            {

                String filepath = this.openFileDlg.FileName;
                // update the xml file 
                if (!Directory.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder)))
                {
                    Directory.CreateDirectory(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder));

                }

                if (!Directory.Exists(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName)))
                {
                    Directory.CreateDirectory(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName));

                }

                File.Copy(filepath, Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, this.openFileDlg.SafeFileName), true);

                Image image = Bitmap.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, this.openFileDlg.SafeFileName));

                // Now resized to the thumbillwindow

                MessageBox.Show("Please wait this will take some time !!!");
                Image resized = GeneralOpt.ImageResize(image, GeneralOpt.ThumBillWidth, GeneralOpt.ThumbillHeight);
                resized.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppThumbillFolderName, this.openFileDlg.SafeFileName));
                this.PdfBgImageBox.Image = resized;
                // update the xmlfile
                //


                XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
                if (!docu.Root.IsEmpty)
                {
                    XElement bgGround = docu.Root.Element("BgImages");
                    if (bgGround == null)
                    {
                        docu.Root.Add(new XElement("BgImages", new XElement("PdfWindow", this.openFileDlg.SafeFileName)));

                    }
                    else
                    {
                        XElement main = bgGround.Element("PdfWindow");
                        if (main == null)
                        {
                            bgGround.Add(new XElement("PdfWindow", this.openFileDlg.SafeFileName));

                        }
                        else
                        {
                            main.SetValue(this.openFileDlg.SafeFileName);

                        }


                    }


                }
                else
                {

                }
                docu.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));


                GeneralOpt.pictIDChange = 3;

            } // ok pressed










        }

        private void mainBgNoneBtn_Click(object sender, EventArgs e)
        {
            // Set it to none

            if (MessageBox.Show("This will remove the Image !! Continue ??") == System.Windows.Forms.DialogResult.OK)
            {

                XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
                if (docu == null)
                    return;

                if (!docu.Root.IsEmpty)
                {
                    XElement temp = docu.Root.Element("BgImages");
                    if (temp != null)
                    {
                        temp = docu.Root.Element("BgImages").Element("MainWindow");
                        if (temp != null)
                        {
                            MessageBox.Show("deleting");
                            docu.Root.Element("BgImages").Element("MainWindow").Remove();
                            docu.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
                            this.mainBGImageBox.Image = null;
                        }

                    }


                }

                GeneralOpt.pictIDChange = 1;
                

            }
        }

        private void buttonBgNonebtn_Click(object sender, EventArgs e)
        {



            if (MessageBox.Show("This will remove the Image !! Continue ??") == System.Windows.Forms.DialogResult.OK)
            {

                XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
                if (docu == null)
                    return;

                if (!docu.Root.IsEmpty)
                {
                    XElement temp = docu.Root.Element("BgImages");
                    if (temp != null)
                    {
                        temp = docu.Root.Element("BgImages").Element("ButtonWindow");
                        if (temp != null)
                        {
                            
                            docu.Root.Element("BgImages").Element("ButtonWindow").Remove();
                            docu.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
                            this.ButtonBGImageBox.Image = null;
                        }

                    }


                }




                GeneralOpt.pictIDChange = 2;



            }



        }

        private void pdfBgNoneBtn_Click(object sender, EventArgs e)
        {


            if (MessageBox.Show("This will remove the Image !! Continue ??") == System.Windows.Forms.DialogResult.OK)
            {

                XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
                if (docu == null)
                    return;

                if (!docu.Root.IsEmpty)
                {
                    XElement temp = docu.Root.Element("BgImages");
                    if (temp != null)
                    {
                        temp = docu.Root.Element("BgImages").Element("PdfWindow");
                        if (temp != null)
                        {

                            docu.Root.Element("BgImages").Element("PdfWindow").Remove();
                            docu.Save(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
                            this.PdfBgImageBox.Image = null;
                        }

                    }


                }


                GeneralOpt.pictIDChange = 3;

            }

        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
